from datetime import datetime, time

from sqlalchemy import bindparam, func, or_, select, update, delete  # noqa: F401
from sqlalchemy.ext.asyncio import AsyncSession

from module_admin.entity.do.role_do import SysRoleDept  # noqa: F401
from module_kylin.entity.do.trade_do import KylinTrade
from module_kylin.entity.vo.trade_vo import TradeModel, TradePageQueryModel
from utils.page_util import PageUtil


class TradeDao:
    """
    总交易流水数据库操作层
    """

    @classmethod
    async def get_trade_detail_by_id(cls, db: AsyncSession, id: int):
        """
        根据交易id获取交易信息

        :param db: orm对象
        :param id: 交易id
        :return: 交易信息对象
        """
        trade_info = (await db.execute(select(KylinTrade).where(KylinTrade.id == id))).scalars().first()

        return trade_info

    @classmethod
    async def get_trade_detail_by_info(cls, db: AsyncSession, trade: TradeModel):
        """
        根据参数配置参数获取参数配置信息

        :param db: orm对象
        :param trade: 参数配置参数对象
        :return: 参数配置信息对象
        """
        trade_info = (
            (
                await db.execute(
                    select(KylinTrade).where(
                        KylinTrade.name == trade.name if trade.name else True,
                        KylinTrade.trade_id == trade.trade_id if trade.trade_id else True,
                    )
                )
            )
            .scalars()
            .first()
        )
        return trade_info

    @classmethod
    async def get_trade_list(cls, db: AsyncSession, query_object: TradePageQueryModel, is_page: bool = False):
        """
        根据查询参数获取成交列表信息

        :param db: orm对象
        :param query_object: 查询参数对象
        :param is_page: 是否开启分页
        :return: 参数配置列表信息对象
        """
        query = (
            select(KylinTrade)
            .where(
                KylinTrade.name.like(f'%{query_object.name}%') if query_object.name else True,
                KylinTrade.direction.like(f'%{query_object.direction}%') if query_object.direction else True,
                KylinTrade.offset == query_object.offset if query_object.offset else True,
                KylinTrade.date.between(
                    datetime.combine(datetime.strptime(query_object.begin_time, '%Y-%m-%d'), time(00, 00, 00)),
                    datetime.combine(datetime.strptime(query_object.end_time, '%Y-%m-%d'), time(23, 59, 59)),
                )
                if query_object.begin_time and query_object.end_time
                else True,
            )
            .order_by(KylinTrade.id)
            .distinct()
        )
        trade_list = await PageUtil.paginate(db, query, query_object.page_num, query_object.page_size, is_page)

        return trade_list

    @classmethod
    async def get_trade_all(cls, db: AsyncSession):
        """
        根据查询参数获取成交列表信息

        :param db: orm对象
        :return: 参数配置列表信息对象
        """
        trade_list = (await db.execute(select(KylinTrade))).scalars().all()
        return trade_list

    @classmethod
    async def add_trade(cls, db: AsyncSession, trade: TradeModel):
        """
        新增交易信息

        :param db: orm对象
        :param trade: 交易信息对象
        :return: 新增交易信息对象
        """
        db_trade = KylinTrade(**trade.model_dump())
        db.add(db_trade)
        await db.flush()
        return trade

    @classmethod
    async def edit_trade(cls, db: AsyncSession, trade: dict):
        """
        编辑交易信息

        :param db: orm对象
        :param trade: 交易信息对象
        :return: 编辑交易信息对象
        """
        await db.execute(update(KylinTrade), [trade])

    @classmethod
    async def delete_trade(cls, db: AsyncSession, trade: TradeModel):
        """
        删除参数配置数据库操作

        :param db: orm对象
        :param trade: 参数配置对象
        :return:
        """
        await db.execute(delete(KylinTrade).where(KylinTrade.id.in_([trade.id])))
